package com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationroom;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationroom.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationroom.AccommodationRoomDO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationroom.AccommodationRoomConvert;
import com.nanhai.buddhist.college.module.collegebase.service.accommodationroom.AccommodationRoomService;

@Api(tags = "管理后台 - 房间")
@RestController
@RequestMapping("/collegebase/accommodation-room")
@Validated
public class AccommodationRoomController {

    @Resource
    private AccommodationRoomService accommodationRoomService;

    @PostMapping("/create")
    @ApiOperation("创建房间")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:create')")
    public CommonResult<String> createAccommodationRoom(@Valid @RequestBody AccommodationRoomCreateReqVO createReqVO) {
        return success(accommodationRoomService.createAccommodationRoom(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新房间")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:update')")
    public CommonResult<Boolean> updateAccommodationRoom(@Valid @RequestBody AccommodationRoomUpdateReqVO updateReqVO) {
        accommodationRoomService.updateAccommodationRoom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除房间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:delete')")
    public CommonResult<Boolean> deleteAccommodationRoom(@RequestParam("id") String id) {
        accommodationRoomService.deleteAccommodationRoom(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得房间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:query')")
    public CommonResult<AccommodationRoomRespVO> getAccommodationRoom(@RequestParam("id") String id) {
        AccommodationRoomDO accommodationRoom = accommodationRoomService.getAccommodationRoom(id);
        return success(AccommodationRoomConvert.INSTANCE.convert(accommodationRoom));
    }

    @GetMapping("/list")
    @ApiOperation("获得房间列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:query')")
    public CommonResult<List<AccommodationRoomRespVO>> getAccommodationRoomList(@RequestParam("ids") Collection<String> ids) {
        List<AccommodationRoomDO> list = accommodationRoomService.getAccommodationRoomList(ids);
        return success(AccommodationRoomConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得房间分页")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:query')")
    public CommonResult<PageResult<AccommodationRoomRespVO>> getAccommodationRoomPage(@Valid AccommodationRoomPageReqVO pageVO) {
        PageResult<AccommodationRoomDO> pageResult = accommodationRoomService.getAccommodationRoomPage(pageVO);
        return success(AccommodationRoomConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出房间 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:export')")
    @OperateLog(type = EXPORT)
    public void exportAccommodationRoomExcel(@Valid AccommodationRoomExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccommodationRoomDO> list = accommodationRoomService.getAccommodationRoomList(exportReqVO);
        // 导出 Excel
        List<AccommodationRoomExcelVO> datas = AccommodationRoomConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "房间.xls", "数据", AccommodationRoomExcelVO.class, datas);
    }

    @GetMapping("/byDepartment/list")
    @ApiOperation("根据系部获取房间列表")
    @ApiImplicitParam(name = "departmentId", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:accommodation-room:query')")
    public CommonResult<List<AccommodationRoomRespVO>> getAccommodationRoomListByDepartmentId(@RequestParam("departmentId")String departmentId) {
        List<AccommodationRoomDO> list = accommodationRoomService.getAccommodationRoomListByDepartmentId(departmentId);
        return success(AccommodationRoomConvert.INSTANCE.convertList(list));
    }

}
